package Controller;

import Util.Constants;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *徐韬
 *2021/10/9
 *9:22
 */
@WebServlet("/user")
public class UserController extends BaseController {
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);
        if(user!=null){
            request.getSession().setAttribute("user", user);
            return Constants.REDIRECT+"/index.jsp";
        }else {
            return Constants.REDIRECT+"/login.jsp";
        }



    }
    public String checkPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oldPwd = request.getParameter("oldPwd");
        System.out.println(oldPwd);
        User user = (User) request.getSession().getAttribute("user");
        if(oldPwd.equals(user.getuPassword())){
            return "1";
        }else{
            return "0";
        }



    }
    public String update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String newPassword = request.getParameter("newPassword");
        User user = (User) request.getSession().getAttribute("user");
        UserService userService = new UserServiceImpl();
        int i = userService.update(newPassword, user.getuAccount());
        System.out.println(i);
        return String.valueOf(i);


    }

}
